﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
namespace SaaS
{

    public partial class AddWorkFlow : System.Web.UI.Page
    {
        string[] defaultWF = {"checkout","User Define"};
        string[] dcheckout = { "VerifyItems", "CalculateShippingCost", "printMessage" };
        string[] wService = { "VerifyItems", "CalculateShippingCost", "CalculateTotal", "Discount", "printMessage" };
        XmlDocument xmldoc = new XmlDocument();
        XmlDocument xmluser = new XmlDocument();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(DropDownList_DefaultWorkflowList.Items.Count==0)
              for (int i = 0; i < defaultWF.Length; i++ )
                DropDownList_DefaultWorkflowList.Items.Add(defaultWF[i]);

            if (DropDownList_WebserviceList.Items.Count==0)
            {
                WebServiceInvoker invoker = new WebServiceInvoker(new Uri("http://localhost:49190/checkOut.asmx"));
                foreach (string service in invoker.AvailableServices)
                {
                    List<string> methods = invoker.EnumerateServiceMethods(service);
                    foreach (string method in methods)
                        DropDownList_WebserviceList.Items.Add(method);
                }
            }
            //Session["invoker"] = invoker;
           try
           {
            xmldoc.Load(Server.MapPath(@"App_Data\tempwork.xml"));
            }
            catch (System.Exception ex)
           {
            string temp  = "<checkout></checkout>";
            xmldoc.LoadXml(temp);
            xmldoc.Save(Server.MapPath(@"App_Data\tempwork.xml"));
            }

           try
           {
               xmluser.Load(Server.MapPath(@"App_Data\tempuser.xml"));
           }
           catch (System.Exception ex)
           {
               string temp = "<userworkflow></userworkflow>";
               xmluser.LoadXml(temp);
               xmluser.Save(Server.MapPath(@"App_Data\tempuser.xml"));
           }

           if (DropDownList_userWorkflow.Items.Count == 0)
               for (int i = 0; i < xmluser.DocumentElement.ChildNodes.Count; i++)
                   DropDownList_userWorkflow.Items.Add(xmluser.DocumentElement.ChildNodes[i].Name);

           Label_notification.Text = "";

        }

        protected void DropDownList_DefaultWorkflowList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void DropDownList_userWorkflow_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                xmluser.Load(Server.MapPath(@"App_Data\tempuser.xml"));
            }
            catch (System.Exception ex)
            {
                return;
            }

            XmlDocument tempxml = new XmlDocument();
            XmlElement tempnode = tempxml.CreateElement(xmluser.DocumentElement.ChildNodes[DropDownList_userWorkflow.SelectedIndex].Name);
            tempnode.InnerXml = xmluser.DocumentElement.ChildNodes[DropDownList_userWorkflow.SelectedIndex].InnerXml;
            tempxml.AppendChild(tempnode);
            tempxml.Save(Server.MapPath(@"App_Data\tempxml.xml"));
            DataSet dateset = new DataSet();
            dateset.ReadXml(Server.MapPath(@"App_Data\tempxml.xml"));
            this.GridView_WebService.DataMember = "service";
            this.GridView_WebService.DataSource = dateset;
            this.GridView_WebService.DataBind();
        }

        protected void Button_Add_Click(object sender, EventArgs e)
        {
            if (TextBox_OrderNum.Text.ToString() == "")
                return;

            try
            {
                xmldoc.Load(Server.MapPath(@"App_Data\tempwork.xml"));
            }
            catch (System.Exception ex)
            {
                return;
            }

            XmlElement no_service = xmldoc.CreateElement("service");
            XmlElement no_serviceName = xmldoc.CreateElement("serviceName");
            XmlElement no_serviceIndx = xmldoc.CreateElement("serviceIndex");
            XmlElement no_orderNum = xmldoc.CreateElement("OrderNumber");

            WebServiceInvoker invoker = new WebServiceInvoker(new Uri("http://localhost:49190/checkOut.asmx"));
            List<string> methods = invoker.EnumerateServiceMethods("checkOut");
            no_serviceName.InnerText = methods[DropDownList_WebserviceList.SelectedIndex];
            no_serviceIndx.InnerText = DropDownList_WebserviceList.SelectedIndex.ToString();
            no_orderNum.InnerText = TextBox_OrderNum.Text.ToString();

            no_service.AppendChild(no_serviceName);
            no_service.AppendChild(no_serviceIndx);
            no_service.AppendChild(no_orderNum);
            xmldoc.DocumentElement.AppendChild(no_service);
            xmldoc.Save(Server.MapPath(@"App_Data\tempwork.xml"));

            DataSet dateset = new DataSet();
            dateset.ReadXml(Server.MapPath("~/App_Data/tempwork.xml"));
            this.GridView_WebService.DataMember = "service";
            this.GridView_WebService.DataSource = dateset;
            this.GridView_WebService.DataBind();
        }

        protected void Button_AddWorkFlow_Click(object sender, EventArgs e)
        {
            if (TextBox_workflowName.Text.ToString() == "")
                return;
            xmldoc.Load(Server.MapPath("~/App_Data/tempwork.xml"));

            if (xmldoc.DocumentElement.ChildNodes.Count == 0)
            {
                Label_notification.Text = "Please Add Services!";
                this.GridView_WebService.DataSource = "";
                this.GridView_WebService.DataBind();
                return;
            }
            xmluser.Load(Server.MapPath(@"App_Data\tempuser.xml"));
            XmlElement tempnode = xmluser.CreateElement(TextBox_workflowName.Text.ToString());
            tempnode.InnerXml = xmldoc.DocumentElement.InnerXml;
            xmluser.DocumentElement.AppendChild(tempnode);
            xmluser.Save(Server.MapPath(@"App_Data\tempuser.xml"));

            xmldoc.DocumentElement.RemoveAll();
            xmldoc.Save(Server.MapPath(@"App_Data\tempwork.xml"));

            DropDownList_userWorkflow.Items.Clear();
            for (int i = 0; i < xmluser.DocumentElement.ChildNodes.Count; i++)
                DropDownList_userWorkflow.Items.Add(xmluser.DocumentElement.ChildNodes[i].Name);

        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            if (TextBox_webserviceNo.Text.ToString() == "")
                return;

            int Num = int.Parse(TextBox_webserviceNo.Text.ToString());

            try
            {
                xmldoc.Load(Server.MapPath(@"App_Data\tempwork.xml"));
            }
            catch (System.Exception ex)
            {
                return;
            }

            int Count = xmldoc.DocumentElement.ChildNodes.Count;
            if (Num > Count - 1 || Num < 0 )
                return;

            xmldoc.DocumentElement.RemoveChild(xmldoc.GetElementsByTagName("service")[Num]);

            xmldoc.Save(Server.MapPath(@"App_Data\tempwork.xml"));
            DataSet dateset = new DataSet();
            dateset.ReadXml(Server.MapPath("~/App_Data/tempwork.xml"));
            this.GridView_WebService.DataMember = "service";
            this.GridView_WebService.DataSource = dateset;
            this.GridView_WebService.DataBind();
        }

        protected void Button_deleteWorkflow_Click(object sender, EventArgs e)
        {

            if (TextBox_workflowName.Text.ToString() == "")
                return;
            xmluser.Load(Server.MapPath(@"App_Data\tempuser.xml"));
            try
            {
                xmluser.DocumentElement.RemoveChild(xmluser.GetElementsByTagName(TextBox_workflowName.Text.ToString())[0]);
            }
            catch (System.Exception ex)
            {
                return;
            }
            xmluser.Save(Server.MapPath(@"App_Data\tempuser.xml"));
            DropDownList_userWorkflow.Items.Clear();
            for (int i = 0; i < xmluser.DocumentElement.ChildNodes.Count; i++)
                DropDownList_userWorkflow.Items.Add(xmluser.DocumentElement.ChildNodes[i].Name);
        }

        protected void Button_Invoke_Click(object sender, EventArgs e)
        {
            WebServiceInvoker invoker = new WebServiceInvoker(new Uri("http://localhost:49190/checkOut.asmx"));
            List<string> methods = invoker.EnumerateServiceMethods("checkOut");
            try
            {
                xmluser.Save(Server.MapPath(@"App_Data\tempuser.xml"));
            }
            catch (System.Exception ex)
            {
                return;
            }

            XmlNode node = xmluser.DocumentElement.ChildNodes[DropDownList_userWorkflow.SelectedIndex];
            string[] args = TextBox_Input.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            XmlNode serviceNode = node.ChildNodes[0];
            string result = invoker.InvokeMethod<string>("checkOut", serviceNode.FirstChild.InnerText, args);
            int Count = node.ChildNodes.Count;
            for (int i = 1; i < Count;i++ )
            {
                serviceNode = node.ChildNodes[i];
                args = result.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                result = invoker.InvokeMethod<string>("checkOut", serviceNode.FirstChild.InnerText, args);
            }
            label_Output.Text = result;
        }

        protected void Button_Edit_Click(object sender, EventArgs e)
        {
            WebServiceInvoker invoker = new WebServiceInvoker(new Uri("http://localhost:49190/checkOut.asmx"));
            List<string> methods = invoker.EnumerateServiceMethods("checkOut");

            try
            {
                xmluser.Load(Server.MapPath(@"App_Data\tempuser.xml"));
            }
            catch (System.Exception ex)
            {
                return;
            }

            XmlDocument tempxml = new XmlDocument();
            XmlElement tempnode = tempxml.CreateElement(xmluser.DocumentElement.ChildNodes[DropDownList_userWorkflow.SelectedIndex].Name);
            tempnode.InnerXml = xmluser.DocumentElement.ChildNodes[DropDownList_userWorkflow.SelectedIndex].InnerXml;
            tempxml.AppendChild(tempnode);

            tempxml.DocumentElement.ChildNodes[int.Parse(TextBox_webserviceNo.Text)].FirstChild.InnerText = methods[DropDownList_WebserviceList.SelectedIndex];
            tempxml.DocumentElement.ChildNodes[int.Parse(TextBox_webserviceNo.Text)].ChildNodes[1].InnerText = DropDownList_WebserviceList.SelectedIndex.ToString();
            tempxml.DocumentElement.ChildNodes[int.Parse(TextBox_webserviceNo.Text)].ChildNodes[2].InnerText = TextBox_OrderNum.Text;
            tempxml.Save(Server.MapPath(@"App_Data\tempxml.xml"));

            xmluser.DocumentElement.ChildNodes[DropDownList_userWorkflow.SelectedIndex].InnerXml = tempxml.InnerXml;
            xmluser.Save(Server.MapPath(@"App_Data\tempuser.xml"));

            DataSet dateset = new DataSet();
            dateset.ReadXml(Server.MapPath(@"App_Data\tempxml.xml"));
            this.GridView_WebService.DataMember = "service";
            this.GridView_WebService.DataSource = dateset;
            this.GridView_WebService.DataBind();

        }

    }
}